<?php
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller {

	//登录页
    public function index(){
    	//登录检测
        if (check_login()) {
            $this->redirect('Admin/Index/index');
        }
        $this->display('Adminlte/pages/examples/login');
    }

    public function login(){
    	if(IS_POST){
			$admin =M("admin");
			if (!I('post.username') || !I('post.password')) {
				$this->error("error name");
			}
			$data['username'] = I('post.username');
			$data['password'] = md5(I('post.password'));
			//$data['status'] = 1;

			$result = $admin->where($data)->find();
			if(!$result){
				$this->error("账号或密码不正确!");
			}
			if($result['status']==0){
				$this->error("您的账号被禁用!");
			}
			
			//登录成功
			$updata['ip'] = getClientIP();
			$updata['login_time'] =time();
			$admin->where('id='.$result['id'])->setField($updata);
			$admin->where('id='.$result['id'])->setInc('login_count');

			//存储SESSION
			session('id',$result['id']);        //用户ID
			session('passid',$result['username']);    //用户账号
			session('login_time',$result['login_time']); //上一次登录时间

			//更新登录时间
			//$admin->where($data)->setField('logindate',date("Y-m-d H-i-s" ,time()));
			
			//存储COOKIE
			cookie('id',$result['id']);
			cookie('username',$result['username']);

			$this->redirect('Admin/index/index');

		}else{
			$this->error("非法请求");
		}
    }

    //注销登录
	public function logout(){
		session(null);
		//cookie(null);
		$this->redirect('Admin/login/index');
	}



    
    
    
}